package com.primb.bench.project.search.presenter;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

import com.primb.bench.common.override.HttpSubscriber;
import com.primb.bench.project.approval.entity.ApprovalListEntity;
import com.primb.bench.project.search.contract.SearchListContract;
import com.primb.bench.project.search.entity.PriceListEntity;
import com.primb.bench.project.search.entity.ReapplyListEntity;
import com.primb.bench.project.search.entity.RequestPriceEntity;
import com.primb.bench.project.search.model.SearchListModelImpl;
import com.primb.netlibs.exception.ApiException;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.HashMap;
import java.util.List;

import cn.lankton.flowlayout.FlowLayout;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/07/17
 */

public class SearchListPresenterImpl<T> implements SearchListContract.Presenter {
    private SearchListModelImpl mModel;
    private SearchListContract.View mView;
    private CompositeSubscription subscription;
    private RequestPriceEntity entity;
    private int code;
    private Context context;

    public SearchListPresenterImpl(Context context, SearchListContract.View mView, int code) {
        this.mView = mView;
        this.context = context;
        this.code = code;
        mModel = new SearchListModelImpl();
        subscription = new CompositeSubscription();
        entity = new RequestPriceEntity();
    }

    @Override
    public void getDataList(RequestPriceEntity entity) {
        mView.showLoading();
        entity.reSet();
        if (code == SearchListContract.KEY_PRICE) {
            Subscription subscribe = mModel.getPriceListInfo(entity).subscribe(new HttpSubscriber<List<PriceListEntity.DataBean>>(context) {
                @Override
                public void onError(ApiException e) {
                    mView.showError(e.getMessage());
                    mView.hideLoading();
//                mView.clearListData();
                    entity.reSet();
                }

                @Override
                public void onCompleted() {
                    mView.hideLoading();
                    entity.setPageAdd();
                }

                @Override
                public void onNext(List<PriceListEntity.DataBean> objects) {
                    mView.clearListData();
                    mView.setDataToList(objects);
                }
            });
            subscription.add(subscribe);
        } else if (code == SearchListContract.KEY_REAPPLY){
            Subscription subscribe = mModel.getReapplyListInfo(entity).subscribe(new HttpSubscriber<List<ReapplyListEntity.DataBean>>(context) {
                @Override
                public void onError(ApiException e) {
                    mView.showError(e.getMessage());
                    mView.hideLoading();
//                mView.clearListData();
                    entity.reSet();
                }

                @Override
                public void onCompleted() {
                    mView.hideLoading();
                    entity.setPageAdd();
                }

                @Override
                public void onNext(List<ReapplyListEntity.DataBean> objects) {
                    mView.clearListData();
                    mView.setDataToList(objects);
                }
            });
            subscription.add(subscribe);
        } else if (code == SearchListContract.KEY_APPROVAL){
            Subscription subscribe = mModel.getApprovalListInfo(entity).subscribe(new HttpSubscriber<List<ApprovalListEntity.DataBean>>(context) {
                @Override
                public void onError(ApiException e) {
                    mView.showError(e.getMessage());
                    mView.hideLoading();
//                mView.clearListData();
                    entity.reSet();
                }

                @Override
                public void onCompleted() {
                    mView.hideLoading();
                    entity.setPageAdd();
                }

                @Override
                public void onNext(List<ApprovalListEntity.DataBean> objects) {
                    mView.clearListData();
                    mView.setDataToList(objects);
                }
            });
            subscription.add(subscribe);
        }
    }

    @Override
    public void loadMoreList() {
        if (code == SearchListContract.KEY_PRICE) {
            Subscription subscribe = mModel.getPriceListInfo(entity).subscribe(new HttpSubscriber<List<PriceListEntity.DataBean>>(context) {
                @Override
                public void onError(ApiException e) {
                    mView.showError(e.getMessage());
                   mView.loadMoreField();
                    entity.reSet();
                }

                @Override
                public void onCompleted() {
                    entity.setPageAdd();
                }

                @Override
                public void onNext(List<PriceListEntity.DataBean> objects) {
                    mView.loadMoreList(objects);
                }
            });
            subscription.add(subscribe);
        } else if (code == SearchListContract.KEY_REAPPLY){
            Subscription subscribe = mModel.getReapplyListInfo(entity).subscribe(new HttpSubscriber<List<ReapplyListEntity.DataBean>>(context) {
                @Override
                public void onError(ApiException e) {
                    mView.showError(e.getMessage());
                    mView.loadMoreField();
                    entity.reSet();
                }

                @Override
                public void onCompleted() {
                    entity.setPageAdd();
                }

                @Override
                public void onNext(List<ReapplyListEntity.DataBean> objects) {
                    mView.loadMoreList(objects);
                }
            });
            subscription.add(subscribe);
        } else if (code == SearchListContract.KEY_APPROVAL){
            Subscription subscribe = mModel.getApprovalListInfo(entity).subscribe(new HttpSubscriber<List<ApprovalListEntity.DataBean>>(context) {
                @Override
                public void onError(ApiException e) {
                    mView.showError(e.getMessage());
                    mView.loadMoreField();
                    entity.reSet();
                }

                @Override
                public void onCompleted() {
                    entity.setPageAdd();
                }

                @Override
                public void onNext(List<ApprovalListEntity.DataBean> objects) {
                    mView.loadMoreList(objects);
                }
            });
            subscription.add(subscribe);
        }
    }

    @Override
    public void showSearchCondition(RequestPriceEntity entity, ViewGroup viewGroup) {
        viewGroup.removeAllViews();
        if (entity.getCustId() != null && !entity.getCustId().equals(""))
            addConditionView("客户号：" + entity.getCustId(), viewGroup);
        if (entity.getCustName() != null && !entity.getCustName().equals(""))
            addConditionView("客户名：" + entity.getCustName(), viewGroup);
        if (entity.getStartDate() != null && !entity.getStartDate().equals(""))
            addConditionView("起始日：" + entity.getStartDate(), viewGroup);
        if (entity.getEndDate() != null && !entity.getEndDate().equals(""))
            addConditionView("结束日：" + entity.getEndDate(), viewGroup);
        if (entity.getLnPrdId() != null && !entity.getLnPrdId().equals(""))
            addConditionView("业务品种：" + entity.getLnPrdName(), viewGroup);
        if (entity.getPriceType() != null && !entity.getPriceType().equals(""))
            addConditionView("定价申请类型：" + (entity.getPriceType().equals("01") ? "贷款定价试算" : "贷款定价申请"), viewGroup);
        if (entity.getLnTrem() != null && !entity.getLnTrem().equals("") &&
                entity.getTremUtil() != null && !entity.getTremUtil().equals(""))
            addConditionView("贷款期限：" + entity.getLnTrem() + (entity.getTremUtil().equals("0") ? "年" : "月"), viewGroup);
        if (entity.getOrgId() != null && !entity.getOrgId().equals(""))
            addConditionView("机构：" + entity.getOrgName(), viewGroup);
        ((FlowLayout) viewGroup).relayoutToAlign();
    }

    @Override
    public void addConditionView(String condition, ViewGroup viewGroup) {
        TextView tvCondition = new TextView(viewGroup.getContext());
        tvCondition.setGravity(Gravity.CENTER);
        tvCondition.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        tvCondition.setTextColor(Color.parseColor("#bcbcbc"));
        tvCondition.setText(condition);
        AutoRelativeLayout.LayoutParams paramsLeft = new AutoRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsLeft.addRule(AutoRelativeLayout.ALIGN_PARENT_LEFT);
        tvCondition.setPadding(10, 15, 10, 15);
        tvCondition.setLayoutParams(paramsLeft);
        viewGroup.addView(tvCondition);
    }

    @Override
    public RequestPriceEntity getRequestPriceEntity() {
        return entity;
    }

    public void setEntity(RequestPriceEntity entity) {
        this.entity = entity;
    }

    @Override
    public void start() {
        getDataList(entity);
    }

    @Override
    public void onRecycle() {
        if (subscription != null) {
            subscription.clear();
        }
    }
}